/*
 * Copyright (c) 2021 Huawei Device Co.,  Ltd.
 * Licensed under the Apache License,  Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,  software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,  either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <unistd.h>
#include "file_ex.h"
#include "system_ability_definition.h"
#include "recptprinter_service.h"
#include "recptprinter_hilog.h"
#include "recptprinter_errors.h"
#include "recptprinter_common.h"
#include "iprinteradaptor.h"
#include "recptprinter_observer_proxy.h"
#include "cashdevice_status.h"

namespace SwanLink {
namespace Finance {
namespace {
// std::string RECPTPRINTER_SERVICE_NAME = "Recptprinter_service";
auto pms = DelayedSpSingleton<RecptprinterService>::GetInstance();
const bool G_REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(
    pms.GetRefPtr());
}

RecptprinterService::RecptprinterService()
    : SystemAbility(FINANCE_RECPTPRINTER_SERVICE_ID, true),
    ready_(false) {
}

RecptprinterService::~RecptprinterService() {}

void RecptprinterService::OnDump()
{ 
}

void RecptprinterService::OnStart()
{
    RECPTPRINTER_HILOGD(RECPTPRINTER_SRV, "Enter");
    if (ready_) {
        RECPTPRINTER_HILOGD(RECPTPRINTER_SRV, "Service is ready,  nothing to do");
        return;
    }
    if (!(Init())) {
        RECPTPRINTER_HILOGE(RECPTPRINTER_SRV, "Call init failed");
        return;
    }

    if (!Publish(this)) {
        RECPTPRINTER_HILOGE(RECPTPRINTER_SRV, "Register to system ability manager failed");
        return;
    }
    ready_ = true;
}

bool RecptprinterService::Init()
{
    RECPTPRINTER_HILOGD(RECPTPRINTER_SRV, "Enter");
    AddStatusList(g_sErr);
    OnInit(CASH_DEVICE_PRINTER, "Printer");
    RECPTPRINTER_HILOGI(RECPTPRINTER_SRV, "Success");
    return true;
}

void RecptprinterService::OnStop()
{
    RECPTPRINTER_HILOGW(RECPTPRINTER_SRV, "Enter");
    OnFree();
    RECPTPRINTER_HILOGW(RECPTPRINTER_SRV, "Success");
}

int32_t RecptprinterService::Dump(int32_t fd, const std::vector<std::u16string> &args)
{
    return ERR_OK;
}


int RecptprinterService::GetPrintFormat(int handle, PrintFormatType ftt)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    IPrinterAdaptor *printer = static_cast<IPrinterAdaptor*>(adaptor.get());
    return printer->GetPrintFormat(ftt);
}

void RecptprinterService::SetPrintFormat(int handle, PrintFormatType ftt, int value)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return;
    IPrinterAdaptor *printer = static_cast<IPrinterAdaptor*>(adaptor.get());
    return printer->SetPrintFormat(ftt, value);
}

void RecptprinterService::GetDeviceSpec(int handle, DeviceSpec &spec)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return;
    IPrinterAdaptor *printer = static_cast<IPrinterAdaptor*>(adaptor.get());
    return printer->GetDeviceSpec(spec);
}

int RecptprinterService::FeedPaper(int handle, int line)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    IPrinterAdaptor *printer = static_cast<IPrinterAdaptor*>(adaptor.get());
    return printer->FeedPaper(line);
}

int RecptprinterService::CutPaper(int handle, int line, int mode)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    IPrinterAdaptor *printer = static_cast<IPrinterAdaptor*>(adaptor.get());
    return printer->CutPaper(line, mode);
}

int RecptprinterService::AddText(int handle, int x, int y, const std::string& text)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    IPrinterAdaptor *printer = static_cast<IPrinterAdaptor*>(adaptor.get());
    return printer->AddText(x, y, text);
}

int RecptprinterService::AddBitmap(int handle, int x, int y, const std::string& lpath)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    IPrinterAdaptor *printer = static_cast<IPrinterAdaptor*>(adaptor.get());
    return printer->AddBitmap(x, y, lpath);
}
    
int RecptprinterService::AddQRCode(int handle, int x, int y, int w, const std::string& qr)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    IPrinterAdaptor *printer = static_cast<IPrinterAdaptor*>(adaptor.get());
    return printer->AddQRCode(x, y, w, qr);
}

int RecptprinterService::AddBarCode(int handle,  int x,  int y,  BarCodeSpec &spec,  const std::string& bar)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    IPrinterAdaptor *printer = static_cast<IPrinterAdaptor*>(adaptor.get());
    return printer->AddBarCode(x, y, spec, bar);
}

void RecptprinterService::StartPrint(int handle, int timeout, PrintListener *listener)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return;
    IPrinterAdaptor *printer = static_cast<IPrinterAdaptor*>(adaptor.get());
    return printer->StartPrint(timeout, listener);
}
}  // namespace Finance
}  // namespace SwanLink
